package com.ruoyi.quartz.task;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.quartz.domain.HatchetAcqJifen;
import com.ruoyi.quartz.service.IHatchetAcqJifenService;
import com.ruoyi.quartz.util.Md5Utilssss;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask{
    public void ryParams(String params){
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams(){
        System.out.println("执行无参方法");
    }
    
    public void jiFen(){
    	IHatchetAcqJifenService hatchetAcqJifenService = SpringUtils.getBean(IHatchetAcqJifenService.class);
    	MediaType mediaType = MediaType.parse("application/json");
		OkHttpClient httpClient = new OkHttpClient();
		String result = "";
		String deptId = "500367";//公司在通道编号
//		String deptId = "500359";//公司在通道编号
		String url = "http://web.yunjuhe.vip/data/v1.0/exchange/list";// 兑换积分登陆接口
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 
		ca.setTime(new Date()); //设置时间为当前时间 
		ca.add(Calendar.DATE, -7);
		String  day = sdf.format(ca.getTime());
		try {
			 JSONObject params = new JSONObject();
				params.put("deptId", deptId);
				params.put("fromDate",day);//开始时间
				params.put("toDate", sdf.format(new Date()));//结束时间
				String md5Msg = "deptId=" + deptId + "&fromDate=" + day + "&toDate="
						+ sdf.format(new Date());
				params.put("sign",  Md5Utilssss.MD5(md5Msg));
			RequestBody requestBody = RequestBody.create(mediaType, params.toString());
			Request request1 = new Request.Builder().post(requestBody).url(url).build();
			Response response1 = httpClient.newCall(request1).execute();
			result = response1.body().string();
			response1.body().close();
			// 解析返回数据
			Map mapTypes = JSON.parseObject(result);
			String code = mapTypes.get("code").toString();
			String data = mapTypes.get("data").toString();
			String msg = mapTypes.get("msg").toString();
			// 是1表示失败，0表示成功
			if (code.equals("0") && msg.equals("操作成功")) {
				Map mapTypes1 = JSON.parseObject(data);
				String dataList = mapTypes1.get("dataList").toString();
				JSONArray array1 = JSONArray.fromObject(dataList);
				List<HatchetAcqJifen> list = JSONArray.toList(array1, HatchetAcqJifen.class);// 过时方法
				for (HatchetAcqJifen hatchetAcqJifen : list) {
					//通过订单号查询是否有重复记录,如果存在不做存储
					HatchetAcqJifen acqJifen = hatchetAcqJifenService.selectJifenByOrderId(hatchetAcqJifen.getOrderId());
					if (acqJifen == null) {
						hatchetAcqJifen.setUuid(UUID.randomUUID().toString());
						hatchetAcqJifen.setBizType("2");
						hatchetAcqJifenService.insertHatchetAcqJifen(hatchetAcqJifen);
					}
				}
			} else {
				System.out.println("调用接口操作失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    
    
    
    
}

